<?php
/**
 * DouPHP小程序模块
 * Author: cuihaifeng  qq 366131726 微信同号
 * 网址：www.zbxingke.com www.0533hf.com
 */

$smarty->assign('ur_here', '底部导航栏');

// act操作项的初始化
$act = $check->is_rec($_REQUEST['act']) ? $_REQUEST['act'] : 'default';

// 赋值给模板
$smarty->assign('act', $act);


//获取导航
$sql = "SELECT * FROM " . $GLOBALS['dou']->table('wxapp_nav') . " WHERE type = 'footer' ORDER BY sort ASC";
$query = $GLOBALS['dou']->query($sql);
$nav_list = array();
while ($row = $GLOBALS['dou']->fetch_array($query)) {
    $nav_list[] = $row;
}

// 图片上传
include_once (ROOT_PATH . 'include/upload.class.php');
$images_dir = 'images/wxapp/'; // 文件上传路径，结尾加斜杠
$img = new Upload(ROOT_PATH . $images_dir); // 实例化类文件
if (!file_exists(ROOT_PATH . $images_dir))
    mkdir(ROOT_PATH . $images_dir, 0777);



// 幻灯列表
if ($act == 'default') {
    $smarty->assign('ur_here', $_LANG['mobile_nav']);
    $smarty->assign('action_link', array(
        'text' => $_LANG['nav_add'],
        'href' => 'wxapp.php?rec=footer_nav&act=add'
    ));

    // 赋值给模板
    $smarty->assign('nav_list', $nav_list);
    $smarty->display('wxapp.htm');
} // 导航添加
elseif ($act == 'add') {
    $smarty->assign('ur_here', $_LANG['mobile_nav']);
    $smarty->assign('action_link', array(
        'text' => $_LANG['nav_list'],
        'href' => 'wxapp.php?rec=footer_nav'
    ));

    // CSRF防御令牌生成
    $smarty->assign('token', $firewall->get_token());

    // 赋值给模板
    $smarty->assign('catalog', $dou->get_catalog());
    $smarty->assign('nav_list', $nav_list);
    $smarty->display('wxapp.htm');
} // 导航添加处理
elseif ($act == 'insert') {
    $nav_menu = explode(",", $_POST['nav_menu']);
    $module = $nav_menu[0];
    $guide =  $nav_menu[1];

    $web = $_POST['guide'];

    if (empty($_POST['nav_name']))
        $dou->dou_msg($_LANG['nav_name'] . $_LANG['is_empty']);

    // 图片上传
    if ($_FILES['image']['name'] != ""){
        $image = $images_dir . $img->upload_image('image', $img->create_file_name('wxapp_nav'));
    }

    // CSRF防御令牌验证
    $firewall->check_token($_POST['token']);

    $sql = "INSERT INTO " . $dou->table('wxapp_nav') . " (id, module, nav_name, guide, type, sort,image,web)" . " VALUES (NULL, '$module', '$_POST[nav_name]', '$guide','footer','$_POST[sort]','$image','$web')";
    $dou->query($sql);

    $dou->create_admin_log($_LANG['wxapp'] . ' - ' . $_LANG['nav_add'] . ': ' . $_POST['nav_name']);
    $dou->dou_msg($_LANG['nav_add_succes'], 'wxapp.php?rec=footer_nav');
} // 导航编辑
elseif ($act == 'edit') {
    $smarty->assign('ur_here', $_LANG['mobile_nav']);
    $smarty->assign('action_link', array(
        'text' => $_LANG['nav_list'],
        'href' => 'wxapp.php?rec=footer_nav'
    ));

    // 验证并获取合法的ID
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : '';

    $query = $dou->select($dou->table('wxapp_nav'), '*', '`id` = \'' . $id . '\'');
    $nav_info = $dou->fetch_array($query);

    // CSRF防御令牌生成
    $smarty->assign('token', $firewall->get_token());

    // 格式化数据
    $nav_info['url'] = $nav_info['module'] == 'nav' ? $nav_info['guide'] : "/pages/{$nav_info[module]}/index?{$nav_info[guide]}";

    // 赋值给模板
    $smarty->assign('catalog', $dou->get_catalog($nav_info['module'], $nav_info['guide']));
    $smarty->assign('nav_list', $nav_list);
    $smarty->assign('nav_info', $nav_info);

    $smarty->display('wxapp.htm');
} // 导航编辑处理
elseif ($act == 'update') {


    if (empty($_POST['nav_name']))
        $dou->dou_msg($_LANG['nav_name'] . $_LANG['is_empty']);

    // CSRF防御令牌验证
    $firewall->check_token($_POST['token']);


    /* 判断是站内还是站外导航 */
    if ($_POST['nav_menu']) {
        $nav_menu = explode(',', $_POST['nav_menu']);
        $update = ", module='$nav_menu[0]', guide='$nav_menu[1]'";
    }



    // 图片上传
    if ($_FILES['image']['name'] != ""){
        $image = ", image = '" . $images_dir . $img->upload_image('image', $img->create_file_name('wxapp_nav', $_POST['id'], 'image')) . "'";
    }


    $sql = "update " . $dou->table('wxapp_nav') . " SET nav_name = '$_POST[nav_name]'" . $update . ", sort = '$_POST[sort]', web = '$_POST[guide]' $image WHERE id = '$_POST[id]'";
    $dou->query($sql);
    $dou->create_admin_log($_LANG['wxapp'] . ' - ' . $_LANG['nav_edit'] . ': ' . $_POST['nav_name']);

    $dou->dou_msg($_LANG['nav_edit_succes'], 'wxapp.php?rec=footer_nav');
} // 导航删除
elseif ($act == 'del') {
    // 验证并获取合法的ID
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : $dou->dou_msg($_LANG['illegal'], 'wxapp.php?rec=footer_nav');

    $nav_name = $dou->get_one("SELECT nav_name FROM " . $dou->table('wxapp_nav') . " WHERE id = '$id'");

    if (isset($_POST['confirm']) ? $_POST['confirm'] : '') {
        $dou->create_admin_log($_LANG['mobile'] . ' - ' . $_LANG['nav_del'] . ': ' . $nav_name);
        $dou->delete($dou->table('wxapp_nav'), "id = '$id'", 'wxapp.php?rec=nav');
    } else {
        $_LANG['del_check'] = preg_replace('/d%/Ums', $nav_name, $_LANG['del_check']);
        $dou->dou_msg($_LANG['del_check'], 'wxapp.php?rec=footer_nav', '', '30', "wxapp.php?rec=footer_nav&act=del&id=$id");
    }
}